#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2018/3/5 0005 13:52
# @Author  : Arliki
# @email   : hkdnxycz@outlook.com
# @File    : make_list
import random as rd
import pandas as pd
import os


def get_name():
    return ran_s(rd.randint(5, 15))


def get_age():
    a = rd.randint(1, 100)
    if a <= 5:
        return None
    else:
        return rd.randint(1, 70)


def get_price(pclass):
    if pclass == 3:
        return '%.2f' % (rd.randint(5, 15) + rd.randint(10, 99) / 100)
    elif pclass == 2:
        return '%.2f' % (rd.randint(16, 40) + rd.randint(10, 99) / 100)
    elif pclass == 1:
        return '%.2f' % (rd.randint(41, 80) + rd.randint(10, 99) / 100)


def ran_s(num):
    letter = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
              'v', 'w', 'x', 'y', 'z', '.']
    begin = 1
    name = ''
    while begin <= num:
        a = rd.choice(letter)
        name = name + a
        begin += 1
    return name


def get_cabin():
    b = '%s%d' % (rd.choice(['A', 'B', 'C', 'E', 'F', 'G']), rd.randint(1, 150))
    a = rd.randint(1, 10)
    if a <= 7:
        return None
    else:
        return b


def run(pid):
    survived = rd.randint(0, 1)  # 是否被救
    pclass = rd.randint(1, 3)  # 船舱等级
    name = get_name()  # 姓名
    sex = rd.choice(['male', 'female'])  # 性别
    age = get_age()  # 年龄 缺省
    bro = rd.choice([0, 0, 0, 0, 1, 2, 3, 4, 1, 2, 3])  # 兄弟数量
    price = get_price(pclass)  # 船票
    parch = rd.choice([0, 0, 0, 0, 1, 2, 3, 4, 1, 2, 3, 3, 5])  # 父母孩子数
    ticket = rd.randint(100000, 400000)  # 船票编号
    cabin = get_cabin()  # 船舱编号 缺省
    embarked = rd.choice(['A', 'B', 'C', 'E', 'O', 'P', 'Q', 'R', 'S'])  # 登船地点
    make = {'PassengerId': pid, 'Survived': survived, 'Pclass': pclass, 'Name': name, 'Sex': sex, 'Age': age,
            'SibSp': bro,
            'Parch': parch, 'Ticket': ticket, 'Fare': price, 'Cabin': cabin, 'Embarked': embarked}
    data = pd.DataFrame.from_dict(make, orient='index').T
    if os.path.exists('name_list.csv'):
        data.to_csv('name_list.csv', index=False, sep=',', encoding='utf-8', mode='a', header=None)
    else:
        data.to_csv('name_list.csv', index=False, sep=',', encoding='utf-8', mode='a')


if __name__ == '__main__':
    a = 1
    while a < 5000:
        run(a)
        a += 1
